<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片分析API测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin: 20px 0;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
        }
        .log {
            background: #f5f5f5;
            padding: 10px;
            border-radius: 4px;
            margin: 10px 0;
            white-space: pre-wrap;
            font-family: monospace;
            max-height: 300px;
            overflow-y: auto;
        }
        button {
            background: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            margin: 5px;
        }
        button:hover {
            background: #0056b3;
        }
        input[type="file"] {
            margin: 10px 0;
        }
        .image-preview {
            max-width: 200px;
            max-height: 200px;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <h1>图片分析API测试</h1>
    
    <div class="test-section">
        <h3>1. 测试图片分析API</h3>
        <input type="file" id="imageFile" accept="image/*">
        <br>
        <input type="text" id="question" placeholder="请输入问题（可选）" style="width: 300px; margin: 10px 0;">
        <br>
        <button onclick="testImageAnalysis()">测试图片分析</button>
        <div id="imagePreview"></div>
    </div>
    
    <div class="test-section">
        <h3>2. 测试知识问答API</h3>
        <input type="text" id="qaQuestion" placeholder="请输入问题" style="width: 300px; margin: 10px 0;">
        <br>
        <button onclick="testKnowledgeQA()">测试知识问答</button>
    </div>
    
    <div class="test-section">
        <h3>3. 测试连接</h3>
        <button onclick="testConnection()">测试连接</button>
    </div>
    
    <div class="test-section">
        <h3>日志输出</h3>
        <div id="log" class="log"></div>
        <button onclick="clearLog()">清空日志</button>
    </div>

    <script>
        const BASE_URL = 'http://localhost:8083';
        let currentImageData = null;
        
        function log(message) {
            const logDiv = document.getElementById('log');
            const timestamp = new Date().toLocaleTimeString();
            logDiv.textContent += `[${timestamp}] ${message}\n`;
            logDiv.scrollTop = logDiv.scrollHeight;
        }
        
        function clearLog() {
            document.getElementById('log').textContent = '';
        }
        
        // 图片文件选择处理
        document.getElementById('imageFile').addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    currentImageData = e.target.result;
                    const preview = document.getElementById('imagePreview');
                    preview.innerHTML = `<img src="${currentImageData}" class="image-preview">`;
                    log(`图片已选择: ${file.name} (${file.size} bytes)`);
                };
                reader.readAsDataURL(file);
            }
        });
        
        // 测试图片分析API
        async function testImageAnalysis() {
            if (!currentImageData) {
                log('请先选择图片');
                return;
            }
            
            const question = document.getElementById('question').value || '请分析这张图片';
            log(`开始测试图片分析API...`);
            log(`问题: ${question}`);
            log(`图片数据长度: ${currentImageData.length}`);
            
            try {
                const response = await fetch(`${BASE_URL}/api/ai/miniprogram/image/analysis`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        userId: 12345,
                        imageData: currentImageData,
                        question: question
                    })
                });
                
                log(`响应状态: ${response.status}`);
                
                if (response.ok) {
                    const result = await response.json();
                    log(`响应成功: ${JSON.stringify(result, null, 2)}`);
                } else {
                    const errorText = await response.text();
                    log(`响应失败: ${errorText}`);
                }
            } catch (error) {
                log(`请求异常: ${error.message}`);
            }
        }
        
        // 测试知识问答API
        async function testKnowledgeQA() {
            const question = document.getElementById('qaQuestion').value;
            if (!question) {
                log('请输入问题');
                return;
            }
            
            log(`开始测试知识问答API...`);
            log(`问题: ${question}`);
            
            try {
                const response = await fetch(`${BASE_URL}/api/ai/miniprogram/knowledge/qa`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        userId: 12345,
                        question: question
                    })
                });
                
                log(`响应状态: ${response.status}`);
                
                if (response.ok) {
                    const result = await response.json();
                    log(`响应成功: ${JSON.stringify(result, null, 2)}`);
                } else {
                    const errorText = await response.text();
                    log(`响应失败: ${errorText}`);
                }
            } catch (error) {
                log(`请求异常: ${error.message}`);
            }
        }
        
        // 测试连接
        async function testConnection() {
            log('开始测试连接...');
            
            try {
                const response = await fetch(`${BASE_URL}/health`);
                log(`健康检查响应状态: ${response.status}`);
                
                if (response.ok) {
                    const result = await response.json();
                    log(`连接成功: ${JSON.stringify(result, null, 2)}`);
                } else {
                    log(`连接失败: ${response.status}`);
                }
            } catch (error) {
                log(`连接异常: ${error.message}`);
            }
        }
        
        // 页面加载完成
        window.onload = function() {
            log('页面加载完成，开始测试...');
            log(`目标服务器: ${BASE_URL}`);
        };
    </script>
</body>
</html> 